package cn.moukou.sbzz.controller;

import cn.moukou.sbzz.pojo.BankProduct;
import cn.moukou.sbzz.pojo.EnterpriseProduct;
import cn.moukou.sbzz.service.EnterpriseProductService;
import cn.moukou.sbzz.util.JwtUtil;
import cn.moukou.sbzz.util.Resp;
import io.jsonwebtoken.SignatureException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sbzz/api")
public class EnterpriseProductController {

    @Autowired
    EnterpriseProductService enterpriseProductService;

    @PostMapping("/enterprise/product/add")
    public Resp<String> pubEnterpriseProduct(HttpServletRequest request,
                                             @RequestParam("title") String title,
                                             @RequestParam("form") String form,
                                             @RequestParam("business") String business,
                                             @RequestParam("pool") String pool,
                                             @RequestParam("keyword") String keyword,
                                             @RequestParam("targetCap") int targetCap,
                                             @RequestParam("desc") String desc,
                                             @RequestParam("note") String note) throws Exception {
        EnterpriseProduct product = new EnterpriseProduct();
        product.setEid(JwtUtil.getUid(request));
        product.setForm(form);
        product.setBusiness(business);
        product.setPool(pool);
        product.setTitle(title);
        product.setKeyword(keyword);
        product.setTargetCap(targetCap);
        product.setDetail(desc);
        product.setNote(note);
        product.setStatus(0);
        int cnt = enterpriseProductService.pubOneEnterpriseProduct(product);
        if(cnt > 0) {
            return Resp.ok("插入成功");
        } else {
            return Resp.fail("插入失败");
        }
    }

    @GetMapping("/enterprise/product/my")
    public Resp<List<EnterpriseProduct>> getMyEnterpriseProduct(HttpServletRequest request) throws Exception {
        String eid = JwtUtil.getUid(request);
        return Resp.ok(enterpriseProductService.getMyEnterpriseProduct(eid));
    }

    @GetMapping("/enterprise/product/unpass")
    public Resp<List<EnterpriseProduct>> getUnpassEnterpriseProduct() throws Exception {
        return Resp.ok(enterpriseProductService.getUnpassEnterpriseProduct());
    }

    @GetMapping("/enterprise/product/list")
    public Resp<List<EnterpriseProduct>> getEnterpriseProductList() throws Exception {
        return Resp.ok(enterpriseProductService.getEnterpriseProductList());
    }

    @GetMapping("/enterprise/product/filter")
    public Resp<List<EnterpriseProduct>> filterEnterpriseProduct(HttpServletRequest request,
                                                                 @RequestParam("form") String form,
                                                                 @RequestParam("business") String business,
                                                                 @RequestParam("pool") String pool) throws Exception {
        String token = request.getHeader("sbzz-token");
        String method = request.getMethod();
        if(StringUtils.isEmpty(token)){
            System.out.println(method);
            throw new SignatureException(JwtUtil.getHeader()+ "不能为空");
        }

        Map<String, Object> map = JwtUtil.getInfoFromToken(token);
        if(map.get("role").equals("admin") || map.get("role").equals("bank")) {
            return Resp.ok(enterpriseProductService.getProductByFilter(form, business, pool));
        } else {
            return Resp.fail("无权限");
        }
    }

    @GetMapping("/enterprise/product/one")
    public Resp<EnterpriseProduct> getOneEnterpriseProduct(@RequestParam("id") String id) throws Exception {
        return Resp.ok(enterpriseProductService.getOneProduct(id));
    }

    @PostMapping("/enterprise/product/pass")
    public Resp<String> passEnterpriseProduct(@RequestParam("id") String id) throws Exception {
        if (enterpriseProductService.passEnterpriseProduct(id) > 0) {
            return Resp.ok("审核成功");
        } else {
            return Resp.fail("审核失败");
        }
    }

}
